//
// * Copyright (c) 2002-2009 "Neo Technology,"
// *     Network Engine for Objects in Lund AB [http://neotechnology.com]
// *
// * This file is part of Neo4j.
// * 
// * Neo4j is free software: you can redistribute it and/or modify
// * it under the terms of the GNU Affero General Public License as
// * published by the Free Software Foundation, either version 3 of the
// * License, or (at your option) any later version.
// * 
// * This program is distributed in the hope that it will be useful,
// * but WITHOUT ANY WARRANTY; without even the implied warranty of
// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// * GNU Affero General Public License for more details.
// * 
// * You should have received a copy of the GNU Affero General Public License
// * along with this program. If not, see <http://www.gnu.org/licenses/>.
// 
namespace org.neo4j.kernel.impl.transaction.xaframework
{

	using XAException = javax.transaction.xa.XAException;
	using XAResource = javax.transaction.xa.XAResource;
	using Xid = javax.transaction.xa.Xid;

///
/// <summary> * Help implementation of the XAResource interface. See
/// * <seealso cref="XaConnectionHelpImpl"/> for more information. </summary>
/// 
	public abstract class XaResourceHelpImpl : XaResource
	{
		private int transactionTimeout = 120;
		private XaTransaction xaTx = null;
		private readonly XaResourceManager xaRm;
		private sbyte[] branchId = null;

		protected internal XaResourceHelpImpl(XaResourceManager xaRm, sbyte[] branchId)
		{
			this.xaRm = xaRm;
			this.branchId = branchId;
		}

///    
///     <summary> * If the transaction commited successfully this method will return the
///     * transaction.
///     *  </summary>
///     * <returns> transaction if completed else <CODE>null</CODE> </returns>
///     
		public virtual XaTransaction getCompletedTx()
		{
			return xaTx;
		}

///    
///     <summary> * Should return true if <CODE>xares</CODE> is the same resource as 
///     * <CODE>this</CODE>.
///     *  </summary>
///     * <returns> true if the resource is same </returns>
///     
		public abstract bool isSameRM(XAResource xares);

//JAVA TO VB & C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public void commit(Xid xid, boolean onePhase) throws XAException
		public virtual void commit(Xid xid, bool onePhase)
		{
			xaTx = xaRm.commit(xid, onePhase);
		}

//JAVA TO VB & C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public void end(Xid xid, int flags) throws XAException
		public virtual void end(Xid xid, int flags)
		{
			if (flags == XAResource.TMSUCCESS)
			{
				xaRm.end(this, xid);
			}
			else if (flags == XAResource.TMSUSPEND)
			{
				xaRm.suspend(xid);
			}
			else if (flags == XAResource.TMFAIL)
			{
				xaRm.fail(this, xid);
			}
		}

//JAVA TO VB & C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public void forget(Xid xid) throws XAException
		public virtual void forget(Xid xid)
		{
			xaRm.forget(xid);
		}

		public virtual int getTransactionTimeout()
		{
			return transactionTimeout;
		}

		public virtual bool setTransactionTimeout(int timeout)
		{
			transactionTimeout = timeout;
			return true;
		}

//JAVA TO VB & C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public int prepare(Xid xid) throws XAException
		public virtual int prepare(Xid xid)
		{
			return xaRm.prepare(xid);
		}

//JAVA TO VB & C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public Xid[] recover(int flag) throws XAException
		public virtual Xid[] recover(int flag)
		{
			return xaRm.recover(flag);
		}

//JAVA TO VB & C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public void rollback(Xid xid) throws XAException
		public virtual void rollback(Xid xid)
		{
			xaRm.rollback(xid);
		}

//JAVA TO VB & C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public void start(Xid xid, int flags) throws XAException
		public virtual void start(Xid xid, int flags)
		{
			xaTx = null;
			if (flags == XAResource.TMNOFLAGS)
			{
				xaRm.Start(this, xid);
			}
			else if (flags == XAResource.TMRESUME)
			{
				xaRm.resume(xid);
			}
			else if (flags == XAResource.TMJOIN)
			{
				xaRm.join(this, xid);
			}
			else
			{
				throw new XAException("Unknown flag[" + flags + "]");
			}
		}

		public virtual void setBranchId(sbyte[] branchId)
		{
			this.branchId = branchId;
		}

		public virtual sbyte[] getBranchId()
		{
			return this.branchId;
		}
	}
}